﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace JLL.China.Infrastructure.Repositories.Banner
{
    public class BannerRepository : SQLRepositoryBase<Model.Banner, int>, IBannerRepository
    {
        public override IList<Model.Banner> FindAll()
        {
            throw new NotImplementedException();
        }

        public IList<Model.Banner> FindByGroupFid(decimal id)
        {
            var items = from m in DataContext.Banners
                        where m.BannerGroupFID.Equals(id) && m.LanguageFID.Equals(1)
                        orderby m.Sequence ascending 
                        select m;
            var banners = items.ToList();
            
                var result = new List<Model.Banner>();
                foreach (var n in banners)
                {
                    result.Add(BannerFactory.FromLINQObject(n));
                }
                return result;
            
        }
        public IList<Model.Banner> FindAllBanner(string Type)
        {
            var items = from m in DataContext.Banners
                        where m.BannerGroupFID.Equals(3) && m.Status.Equals(true)
                        && m.Type.Equals(Type)
                        select m;
                        
            var banners = items.ToList();

            var result = new List<Model.Banner>();
            foreach (var n in banners)
            {
                result.Add(BannerFactory.FromLINQObject(n));
            }
            return result;
        } 
        public Model.Banner FindByID_LanguageFid(decimal? id,byte languageFid)
        {
            Model.Banner item;
            if (id != null) //get home banner
            {
                var i = (from m in DataContext.Banners
                         where m.Banner_ID.Equals(id) && m.LanguageFID.Equals(languageFid)
                         orderby m.Sequence ascending
                         select m).SingleOrDefault();
                item = BannerFactory.FromLINQObject(i);
            }
            else //get global banner
            {
                var i = (from m in DataContext.Banners
                         where m.BannerGroupFID.Equals(2) && m.LanguageFID.Equals(languageFid)
                         orderby m.Sequence ascending
                         select m).SingleOrDefault();
                item = BannerFactory.FromLINQObject(i);
            }
            return item;
        }
        public void ChangeGroupStatus(string arrayId, bool? status)
        {
            status = (status == null) ? false : status;
            DataContext.sp_banner_group_change_status(arrayId, status);
        }

        public void RemoveSelectedGroup(string arrayId)
        {
            DataContext.sp_banner_group_remove_selected(arrayId);
        }
      
        public IList<Model.Banner> FindByGroupFidAndLanguage(decimal? id, byte language)
        {
            if (id==null)
            {
                return null;
            }
            var items = from m in DataContext.Banners
                        where m.BannerGroupFID.Equals(id) && m.LanguageFID.Equals(language)
                        orderby m.Sequence ascending 
                        select m ;

            var banners = items.ToList();

            var result = new List<Model.Banner>();
            foreach (var n in banners)
            {
                result.Add(BannerFactory.FromLINQObject(n));
            }
            return result;
        }

        public override Model.Banner FindBy(int key)
        {
            throw new NotImplementedException();
        }

        public decimal CreateBanner(Model.Banner banner)
        {
            try
            {
                return DataContext.sp_banner_create(banner.Banner_ID, banner.BannerUrl, banner.Link, banner.Status,
                                                    banner.Title, banner.Content, banner.Sequence, banner.BannerGroupFID,
                                                    banner.Type, banner.LanguageFID,banner.ParentID,banner.DynamicType);
            }
            catch (Exception exception)
            {

                return -1;
            }

        }
        public decimal UpdateBanner(Model.Banner banner)
        {
            try
            {
                return DataContext.sp_banner_update(banner.Banner_ID, banner.BannerUrl, banner.Link, banner.Status,
                                                    banner.Title, banner.Content, banner.Sequence, banner.BannerGroupFID,
                                                    banner.Type, banner.LanguageFID, banner.ParentID, banner.DynamicType);
            }
            catch (Exception)
            {
                return -1;
            }

        }
        public decimal UpdateBannerParent(Model.Banner banner)
        {
            try
            {
                return DataContext.sp_banner_parent_update(banner.Banner_ID, banner.Title, banner.DynamicType,banner.Status);

            }
            catch (Exception)
            {
                return -1;
            }

        }
        public decimal DeleteBanner(decimal? bannerId)
        {
            if (bannerId == null) return -1;
            return DataContext.sp_banner_delete(bannerId);
        }

        public void ChangeStatus(string arrIds, bool status)
        {
            DataContext.sp_banner_change_status(arrIds, status);
        }

        public void RemoveSelected(string arrIds)
        {
            DataContext.sp_banner_remove_selected(arrIds);
        }

        protected override void PersistDeletedItem(Model.Banner item)
        {
            var banner = DataContext.Banners.SingleOrDefault(x => x.Banner_ID == item.ID);
            if (banner != null)
                DataContext.Banners.DeleteOnSubmit(banner);
//            DataContext.SubmitChanges();
        }

        protected override void PersistNewItem(Model.Banner item)
        {
            var banner = BannerFactory.ToLINQObject(item);
            DataContext.Banners.InsertOnSubmit(banner);
            //DataContext.SubmitChanges();
        }

        protected override void PersistUpdatedItem(Model.Banner item)
        {
            var existBanner = DataContext.Banners.SingleOrDefault(x => x.Banner_ID == item.ID);
            if (existBanner != null)
            {
                var banner = item;
                existBanner.BannerUrl = banner.BannerUrl;
                existBanner.Link = banner.Link;
                existBanner.Status = banner.Status;
                existBanner.Title = banner.Title;
                existBanner.Sequence = banner.Sequence;
                existBanner.BannerGroupFID = banner.BannerGroupFID;
            }
            else
            {
                throw new Exception("Account not found to update");
            }
            //DataContext.SubmitChanges();
        }
    }
}
